<?php

namespace App\Models\Cruise;

use App\Models\BaseModel;
use App\Models\Common\ResourcesModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopUserIdentityCardModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;

class CruiseCheckErrorModel extends BaseModel
{
    use KfUid, KfSoftDeletes;

    protected $table    = 'cruise_check_errors';
    protected $fillable = [
        'shopId',
        'cruiseCheckDetailId',
        'invalidType',
        'conditionExplain',
        'dealResult',
        'againstLevel',
        'againstCategory',
        'status',
        'dealUid',
        'dealTime',
        'isDeleted',
    ];

    const INVALID_TYPE_STOP   = 1;
    const INVALID_TYPE_REJECT = 2;
    const INVALID_TYPE_MISS   = 3;

    const INVALID_TYPE_TEXT = [
        self::INVALID_TYPE_STOP   => '门店已暂停营业',
        self::INVALID_TYPE_REJECT => '门店拒绝稽核',
        self::INVALID_TYPE_MISS   => '任务生成错误',
    ];

    const AGAINST_CATEGORY_DEFAULT = 0;
    const AGAINST_CATEGORY_SECRET  = 1;

    const AGAINST_CATEGORY_MAPPING = [
        self::AGAINST_CATEGORY_DEFAULT => '默认',
        self::AGAINST_CATEGORY_SECRET  => '私采',
    ];

    const DEAL_RESULT_UNTREATED     = 0;
    const DEAL_RESULT_NOT           = 1;
    const DEAL_RESULT_BREACH_LETTER = 2;

    const DEAL_RESULT_MAPPING = [
        self::DEAL_RESULT_UNTREATED     => '未处理',
        self::DEAL_RESULT_NOT           => '暂不处理',
        self::DEAL_RESULT_BREACH_LETTER => '开具违约函',
    ];

    const STATUS_DEFAULT = 0;
    const STATUS_COMMIT  = 1;

    const AGAINST_STATUS_MAPPING = [
        self::STATUS_DEFAULT => '待提交',
        self::STATUS_COMMIT  => '已提交',
    ];


    /**
     * @return HasManyThrough
     */
    public function userIdentityCardRel(): HasManyThrough
    {
        return $this->hasManyThrough(
            OpenShopUserIdentityCardModel::class,
            OpenShopMainProcessModel::class,
            'shopId',
            'orderNo',
            'shopId',
            'orderNo');
    }

    /**
     * 关联巡检报告表
     * @return HasOne
     */
    public function cruiseCheckDetailRel(): HasOne
    {
        return $this->hasOne(CruiseCheckDetailModel::class, 'id', 'cruiseCheckDetailId');
    }

    /**
     * 关联巡检报告表
     * @return HasOne
     */
    public function cruiseCheckAgainstRel(): HasOne
    {
        return $this->hasOne(CruiseCheckAgainstModel::class, 'businessId', 'id')
            ->where('businessType', CruiseCheckAgainstModel::BUSINESS_TYPE_ERROR);
    }

    /**
     * 修改异常报备单
     * @param $id
     * @param $data
     * @return int
     */
    public static function setCheckErrorsInfoById($id, $data): int
    {
        return self::query()->where(['id' => $id])->update($data);
    }


    /**
     * 关联资源表
     * @return HasMany
     */
    public function resourcesRel(): HasMany
    {
        return $this->hasMany(ResourcesModel::class, 'businessId', 'id')
            ->where('module', ResourcesModel::MODULE_TYPE_CHECK)
            ->where('business', ResourcesModel::BUSINESS_CHECK_ERROR_REPORT_VIDEO)
            ->where('name', 'reportVideo');
    }
}
